﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using WdpaDownloadServiceRef;

public partial class Default2 : System.Web.UI.Page
{
    DownloadLinks[] m_latestDLLinks;

    DownloadLinks[] m_allDLLinks;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated)
        {
            divLoggedInDisplay.Visible = true;
            divLoggedOutDisplay.Visible = false;
            getLatestDownloads();
            getArchivedDownloads();
            checkDownloadCount();

        }
        else
        {
            divLoggedInDisplay.Visible = false;
            divLoggedOutDisplay.Visible = true;

        }
    }

    protected void checkDownloadCount()
    {
        if(m_allDLLinks.Length == 0 && m_latestDLLinks.Length == 0)
        {
            divNoDownloads.Visible = true;

            divDownloads.Visible = false;
        }
    }

    protected void getLatestDownloads()
    {
        WdpaDownloadService WdpaDownloadService1 = new WdpaDownloadService();

        //DownloadLinks[] latestDLLinks;

        m_latestDLLinks = WdpaDownloadService1.getLatestDownloadLink(User.Identity.Name);

        if (m_latestDLLinks.Length != 0)
        {

            StringBuilder sbDownloadLinks = new StringBuilder();

            sbDownloadLinks.Append("<p class='faq_qst colps_head'>View This Weeks Downloads</p>");

            sbDownloadLinks.Append("<div class='faq_answr colps_body'>");

            sbDownloadLinks.Append("<table width='100%' cellpadding='5' cellspacing='0' border='0'>");

            //sbDownloadLinks.Append("<tr>");

            //sbDownloadLinks.Append("<td>");

            //sbDownloadLinks.Append("<strong>Date</strong>");

            //sbDownloadLinks.Append("</td>");

            //sbDownloadLinks.Append("<td>");

            //sbDownloadLinks.Append("<strong>Shape Files</strong>");

            //sbDownloadLinks.Append("</td>");

            //sbDownloadLinks.Append("<td>");

            //sbDownloadLinks.Append("<strong>KML Files</strong>");

            //sbDownloadLinks.Append("</td>");

            //sbDownloadLinks.Append("</tr>");


            for (int i = 0; i < m_latestDLLinks.Length; i++)
            {
                sbDownloadLinks.Append("<tr>");

                sbDownloadLinks.Append("<td valign='top'>");
                sbDownloadLinks.Append(m_latestDLLinks[i].downloaddate);
                sbDownloadLinks.Append("</td>");

                sbDownloadLinks.Append("<td valign='top'>");
                sbDownloadLinks.Append("<a href='");
                sbDownloadLinks.Append(m_latestDLLinks[i].shapeURL);
                sbDownloadLinks.Append("'><img src='img/iconDownloadShape.gif' alt='Download ShapeFile' style='margin-top:3px'; /></a>");
                sbDownloadLinks.Append("</td>");

                sbDownloadLinks.Append("<td valign='top'>");

                for (int i2 = 0; i2 < m_latestDLLinks[i].kmlURLS.Length; i2++)
                {
                    sbDownloadLinks.Append("<a href='");
                    sbDownloadLinks.Append(m_latestDLLinks[i].kmlURLS[i2].ToString());
                    sbDownloadLinks.Append("'><img src='img/iconDownloadKML.jpg' style='margin:10px' alt='Download KML File' /></a><br />");

                }

                sbDownloadLinks.Append("</td>");

                sbDownloadLinks.Append("</tr>");

                sbDownloadLinks.Append("<tr>");
                sbDownloadLinks.Append("<td colspan='3'>");
                sbDownloadLinks.Append("<hr />");
                sbDownloadLinks.Append("</td>");
                sbDownloadLinks.Append("</tr>");
            }

            sbDownloadLinks.Append("</table>");
            
            sbDownloadLinks.Append("</div>");

            divDownloadLatestContent.InnerHtml = sbDownloadLinks.ToString();

            divNoDownloads.Visible = false;

            divDownloads.Visible = true;
        }
        else
        {

            divDownloadLatestContent.Visible = false;
        }
    }


    protected void getArchivedDownloads()
    {
        WdpaDownloadService WdpaDownloadService1 = new WdpaDownloadService();

        //DownloadLinks[] allDLLinks;

        m_allDLLinks = WdpaDownloadService1.getDownloadLinkArchive(User.Identity.Name);

        if (m_allDLLinks.Length != 0)
        {

            StringBuilder sbDownloadLinks = new StringBuilder();

            sbDownloadLinks.Append("<p class='faq_qst colps_head'>View Archived Downloads</p>");

            sbDownloadLinks.Append("<div class='faq_answr colps_body'>");

            sbDownloadLinks.Append("<table width='100%' cellpadding='5' cellspacing='0' border='0'>");

            //sbDownloadLinks.Append("<tr>");

            //sbDownloadLinks.Append("<td>");

            //sbDownloadLinks.Append("<strong>Date</strong>");

            //sbDownloadLinks.Append("</td>");

            //sbDownloadLinks.Append("<td>");

            //sbDownloadLinks.Append("<strong>ShapeFiles</strong>");

            //sbDownloadLinks.Append("</td>");

            //sbDownloadLinks.Append("<td>");

            //sbDownloadLinks.Append("<strong>KML Files</strong>");

            //sbDownloadLinks.Append("</td>");

            //sbDownloadLinks.Append("</tr>");


            for (int i = 0; i < m_allDLLinks.Length; i++)
            {
                sbDownloadLinks.Append("<tr>");

                sbDownloadLinks.Append("<td valign='top'>");
                sbDownloadLinks.Append(m_allDLLinks[i].downloaddate);
                sbDownloadLinks.Append("</td>");

                sbDownloadLinks.Append("<td valign='top'>");
                sbDownloadLinks.Append("<a href='");
                sbDownloadLinks.Append(m_allDLLinks[i].shapeURL);
                sbDownloadLinks.Append("'><img src='img/iconDownloadShape.gif' alt='Download ShapeFile' style='margin-top:3px'; /></a>");
                sbDownloadLinks.Append("</td>");

                sbDownloadLinks.Append("<td valign='top'>");

                for (int i2 = 0; i2 < m_allDLLinks[i].kmlURLS.Length; i2++)
                {
                    sbDownloadLinks.Append("<a href='");
                    sbDownloadLinks.Append(m_allDLLinks[i].kmlURLS[i2].ToString());
                    sbDownloadLinks.Append("'><img src='img/iconDownloadKML.jpg' style='margin:10px' alt='Download KML File' /></a><br />");

                }

                sbDownloadLinks.Append("</td>");

                sbDownloadLinks.Append("</tr>");

                sbDownloadLinks.Append("<tr>");
                sbDownloadLinks.Append("<td colspan='3'>");
                sbDownloadLinks.Append("<hr />");
                sbDownloadLinks.Append("</td>");
                sbDownloadLinks.Append("</tr>");
            }

            sbDownloadLinks.Append("</table>");

            sbDownloadLinks.Append("</div>");

            divDownloadArchiveContent.InnerHtml = sbDownloadLinks.ToString();

            divNoDownloads.Visible = false;

            divDownloads.Visible = true;
        }
        else
        {

            divDownloadArchiveContent.Visible = false;

        }
    }

}
